package Volume1.ch6;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * 内部类使用demo
 * @Auther jacob
 * @Date 2020/8/12 00:54
 */
public class InnerClassTest {
    public static void main(String[] args) {
        TalkingClock clock = new TalkingClock(1000, true);
        clock.start();

        JOptionPane.showMessageDialog(null, "退出程序");
        System.exit(0);
    }
}

class TalkingClock{
    /**
     * 发布告警的间隔
     */
    private int interval;

    /**
     * 开关铃声标志
     */
    private boolean beep;

    public TalkingClock(int interval, boolean beep) {
        this.interval = interval;
        this.beep = beep;
    }

    public void start(){
        ActionListener listener = new TimePrinter();
        Timer t = new Timer(interval, listener);
        t.start();

    }

    private class TimePrinter implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("当前时间为：" + new Date());
            if (beep){
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }
}